#ifndef CHATWINDOW_H
#define CHATWINDOW_H

#include <QWidget>
#include <QGraphicsScene>
#include <QList>
#include <QEvent>
#include <QSharedPointer>
#if defined (WIN32)
#include <memory>
#else
#include <tr1/memory>
#endif

#include "customevent.h"
#include "messageitem.h"
#include "contactitem.h"

namespace Ui {
    class ChatWindow;
}

class MessageSession;
class QGraphicsView;
class QLabel;
class ChatTextEdit;
class QSortFilterProxyModel;
class ChatTableModel;
class QTableView;
class ChatItemDelegate;

extern int EVENT_CLOSE_CHAT_WINDOW;
class ChatWindow;
class CloseChatWindowEvent: public QEvent
{
public:
    explicit CloseChatWindowEvent(ChatWindow& wnd):QEvent((QEvent::Type)EVENT_CLOSE_CHAT_WINDOW),
    window(wnd){}
    ChatWindow& window;
};

class ChatWindow : public QWidget
{
    Q_OBJECT

public:
    explicit ChatWindow(QWidget *parent = 0);
    explicit ChatWindow(ChatWindow const& val);
    ~ChatWindow();
    ChatWindow& operator=(ChatWindow const& val);
    bool operator==(ChatWindow const& val);
    void SetContactItem(ContactItem& item)
    {
        contact = item;
    }
    void setSession(MessageSession* _session)
    {
        session = _session;
    }

    void setEventHandler(QObject* obj)
    {
        eventHandler = obj;
    }

public slots:

protected:
    ChatWindow& copy(ChatWindow const& val);
    void initialize();
    void initializeTableModel();
    void onMessageReceived(MessageItem& item);
    virtual void	closeEvent ( QCloseEvent * event );
    DECLARE_CUSTOM_EVENT()


protected slots:
            bool event(QEvent *myEvent);
    virtual void  keyPressEvent(QKeyEvent *event);


private:
    QSharedPointer<ChatTableModel>  table;
    QSharedPointer<QSortFilterProxyModel> proxyModel;
    QSharedPointer<ChatItemDelegate> item;
    QObject* eventHandler;
    MessageSession* session;

    Ui::ChatWindow *ui;
    ChatTextEdit* textEditMessage;
    QTableView* tableView;
    QLabel* labelName;
    QGraphicsView* profileView;

    QString key;
    QPixmap profileImage;
    QGraphicsScene scene;
    QString contactName;
    ContactItem contact;
    MessageList messageList;
    bool bFirst;

private slots:
    void on_sendButton_clicked();
};

#endif // CHATWINDOW_H
